package com.bifrii.bo;

import java.util.List;
import com.bifrii.dao.IEventDAO;
import com.bifrii.dao.JDBCEventDAO;
import com.bifrii.dao.UIDGenerator;
import com.bifrii.transferObjects.Event;
import com.bifrii.transferObjects.User;
import com.bifrii.bo.PendingEventManager;

public class EventManager {
	private static IEventDAO event;
	private static EventManager em;
	private static Integer top = 3;
	
	public static synchronized EventManager getInstance(){
		if(em == null)
			em = new EventManager();
		return em;
	}
	
	private EventManager(){
		event = new JDBCEventDAO();
	}
	
	public boolean testEvent(String name){ 
		
		boolean ret = true;
		List<Event> events = event.selectAllEvents();
		for(Event e:events){
			if(e.getName().equals(name))
				ret=false;
		}
		return ret;
	}
	public void newEvent(Event e, Integer idUser){
		e.setOid(UIDGenerator.getInstance().getKey());
		e.setIdEvent(getLastId() + 1);
		e.setIdCreator(idUser);
		event.insert(e);
		PendingEventManager.getInstance().markAsUnread(e.getIdEvent(), idUser);
		PendingEventManager.getInstance().notifyEvent(e);
	}
	
	public List<User> getEventUsers(Integer idEvent){
		
		return event.selectAllUsers(idEvent);
	} 
	public void deleteEvent(Integer idEvent){
		event.delete(idEvent);
	}
	public List<Event> getAllEvents(){
		return event.selectAllEvents();
	}
	public Event getEvent(Integer idEvent){
		return event.select(idEvent);
	}
	public List<Event> getTopEvents(){
		return event.selectTopEvents(top);
	}
	
	public Integer getLastId()
	{
		return event.selectAllEvents().get(0).getIdEvent();
	}


}
